package com.bookstore.common.validation;

/**
 * 验证结果包装类
 * 用于封装验证操作的结果信息
 */
public class ValidationResult {
    
    /**
     * 验证是否通过
     */
    private boolean valid;
    
    /**
     * 错误信息
     */
    private String errorMessage;
    
    /**
     * 错误代码
     */
    private String errorCode;
    
    /**
     * 验证失败的字段名
     */
    private String fieldName;

    private ValidationResult(boolean valid, String errorMessage, String errorCode, String fieldName) {
        this.valid = valid;
        this.errorMessage = errorMessage;
        this.errorCode = errorCode;
        this.fieldName = fieldName;
    }

    /**
     * 创建验证成功的结果
     */
    public static ValidationResult success() {
        return new ValidationResult(true, null, null, null);
    }

    /**
     * 创建验证失败的结果
     */
    public static ValidationResult failure(String errorMessage) {
        return new ValidationResult(false, errorMessage, null, null);
    }

    /**
     * 创建验证失败的结果（带错误代码）
     */
    public static ValidationResult failure(String errorMessage, String errorCode) {
        return new ValidationResult(false, errorMessage, errorCode, null);
    }

    /**
     * 创建验证失败的结果（带字段名）
     */
    public static ValidationResult failure(String errorMessage, String errorCode, String fieldName) {
        return new ValidationResult(false, errorMessage, errorCode, fieldName);
    }

    public boolean isValid() {
        return valid;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public String getErrorCode() {
        return errorCode;
    }

    public String getFieldName() {
        return fieldName;
    }

    @Override
    public String toString() {
        return "ValidationResult{" +
                "valid=" + valid +
                ", errorMessage='" + errorMessage + '\'' +
                ", errorCode='" + errorCode + '\'' +
                ", fieldName='" + fieldName + '\'' +
                '}';
    }
}